package org.example.lanchain.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.lanchain.bean.Appointment;
import org.example.lanchain.mapper.AppointmentMapper;
import org.example.lanchain.service.AppointmentService;
import org.springframework.stereotype.Service;

/**
 * 预约挂号服务实现类
 * 
 * @Service 注解标记这是一个 Spring 服务类，会被自动扫描并注册为 Bean
 * 继承自 MyBatis-Plus 的 ServiceImpl 类，提供基础的 CRUD 实现
 * 实现 AppointmentService 接口，提供预约相关的业务逻辑
 */
@Service
public class AppointmentServiceImpl extends ServiceImpl<AppointmentMapper, Appointment>
        implements AppointmentService {
    
    /**
     * 查询预约记录是否存在
     * 
     * 通过多个条件组合查询，确保预约信息的唯一性：
     * 1. 患者姓名
     * 2. 身份证号
     * 3. 就诊科室
     * 4. 预约日期
     * 5. 预约时间
     *
     * @param appointment 包含查询条件的预约对象
     * @return 返回匹配的预约记录，如果没有找到则返回 null
     */
    @Override
    public Appointment getOne(Appointment appointment) {
        // 创建查询条件包装器
        LambdaQueryWrapper<Appointment> queryWrapper = new LambdaQueryWrapper<>();
        
        // 设置查询条件，使用 Lambda 表达式避免字段名写错
        queryWrapper.eq(Appointment::getUsername, appointment.getUsername())
                   .eq(Appointment::getIdCard, appointment.getIdCard())
                   .eq(Appointment::getDepartment, appointment.getDepartment())
                   .eq(Appointment::getDate, appointment.getDate())
                   .eq(Appointment::getTime, appointment.getTime());
        
        // 执行查询并返回结果
        Appointment appointmentDB = baseMapper.selectOne(queryWrapper);
        return appointmentDB;
    }
}